import com.xizi.Transactional.AccountService2222;
import com.xizi.Transactional.AccountServiceImpl;
import com.xizi.advisor.MyStaticPointcutAdvisor;
import com.xizi.log.MyMethodBeforeAdvice;
import com.xizi.service_constructor.UserService;
import com.xizi.service_constructor.UserServiceImpl;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口
        UserService userService = (UserService) context.getBean("userService");
        userService.select(123, "戏子");
    }

    @Test
    public void test5() {
        // 前置增强
        // 1、实例化bean和增强
        UserService userService = new UserServiceImpl();
        MyMethodBeforeAdvice advice = new MyMethodBeforeAdvice();
        // 2、创建ProxyFactory并设置代理目标和增强
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(userService);
        proxyFactory.addAdvice(advice);
        // 3、生成代理实例
        UserService proxyUser = (UserService) proxyFactory.getProxy();
        proxyUser.select(66, "戏子");
    }

    @Test
    public void test1() {
        // 前置增强
        //创建目标类 只匹配UserServiceImpl类
        UserService userService = new UserServiceImpl();
        // 1、创建增强方法 和切面类
        MyMethodBeforeAdvice advice = new MyMethodBeforeAdvice();
        MyStaticPointcutAdvisor advisor = new MyStaticPointcutAdvisor();
        // 2、创建ProxyFactory并设置代理目标和切面
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(userService);
        // 为切面类提供增强
        advisor.setAdvice(advice);
        //代理工厂设置切面
        proxyFactory.addAdvisor(advisor);
        // 3、生成代理实例
        UserService proxyUser = (UserService) proxyFactory.getProxy();
        //代理用户调用delete方法
        proxyUser.delete();
    }


    @Test
    public void test22() {
        // 基于tx标签的声明式事物
        ApplicationContext ctx = new ClassPathXmlApplicationContext("AopTransactional.xml");
        AccountServiceImpl studentService = ctx.getBean("accountService", AccountServiceImpl.class);
        studentService.save();
    }


    @Test
    public void test122() {
        // 基于tx标签的声明式事物
        ApplicationContext ctx = new ClassPathXmlApplicationContext("AopTransactional22.xml");
        AccountService2222 studentService = ctx.getBean("accountService", AccountService2222.class);
        studentService.save();
    }


}
